package com.page;

import com.utils.ByType;
import com.utils.LogCustom;
import com.utils.XmlReadUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;


public class BaseAction extends TestBaseCase {

    //protected 只在本包之内有权限，可访问
    //protected  WebDriver driver;
    // protected LogCustom logCustom;
    //定位信息图（对象库存储结构）
    protected HashMap<String, Locator> locatorMap;
    public String path = null;
    public InputStream path_inputStream_1;
    public InputStream path_inputStream_2;
    LogCustom logCustom = new LogCustom(this.getClass());

    public void setXmlObjectPath(String path) {
        this.path = path;
    }

    public void setXmlObjectPathForLocator(InputStream path_inputStream) {
        this.path_inputStream_1 = path_inputStream;
    }

    public void setXmlObjectPathForPageURL(InputStream path_inputStream) {
        this.path_inputStream_2 = path_inputStream;
    }


    public void getLocatorMap() {
        XmlReadUtil xmlReadUtil = new XmlReadUtil();
        try {
            //org.webdriver.patatiumwebui.pageObject.PageElement
            if ((path == null || path.isEmpty())) {
                locatorMap = xmlReadUtil.readXMLDocument(path_inputStream_1, this.getClass().getCanonicalName());
            } else {
                locatorMap = xmlReadUtil.readXMLDocument(path, this.getClass().getCanonicalName());
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public void getLocatorMap(String valueName) {
        XmlReadUtil xmlReadUtil = new XmlReadUtil();
        try {
            if ((path == null || path.isEmpty())) {
                locatorMap = xmlReadUtil.readXMLDocument(path_inputStream_1, valueName);
            } else {
                locatorMap = xmlReadUtil.readXMLDocument(path, valueName);
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    static By getBy(ByType byType, Locator locator) {
        switch (byType) {
            case id:
                return By.id(locator.getElement());
            case cssSelector:
                return By.cssSelector(locator.getElement());
            case name:
                return By.name(locator.getElement());
            case xpath:
                return By.xpath(locator.getElement());
            case className:
                return By.className(locator.getElement());
            case tagName:
                return By.tagName(locator.getElement());
            case linkText:
                return By.linkText(locator.getElement());
            case partialLinkText:
                return By.partialLinkText(locator.getElement());
            //return null也可以放到switch外面
            default:
                return null;
        }


    }

    /**
     * 从对象库获取定位信息
     *
     * @param locatorName 对象库名字
     * @return
     * @throws IOException
     */
    public Locator getLocator(String locatorName) {
        Locator locator;
        /**
         * 在对象库通过对象名字查找定位信息
         */
        locator = locatorMap.get(locatorName);
        /**
         * 加入对象库，找不到该定位信息，就创建一个定位信息
         */
        if (locator == null) {
            logCustom.error("没有找到" + locatorName + "页面元素");
        }
        return locator;

    }

    public String getPageURL() {
        String pageURL = null;
        try {
            if (path == null || path.isEmpty()) {
                pageURL = XmlReadUtil.getXmlPageURL(path_inputStream_1, this.getClass().getCanonicalName());
            } else {
                pageURL = XmlReadUtil.getXmlPageURL(path, this.getClass().getCanonicalName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageURL;
    }

    /**
     * 打开浏览器
     *
     * @param url
     */
    public void open(String url) {
        driver.navigate().to(url);
        logCustom.info("打开浏览器，访问" + url + "网址!");
    }

    /***
     * 关闭浏览器窗口
     */
    public void close() {
        driver.close();
        logCustom.info("关闭浏览器窗口");
    }

    /**
     * 退出浏览器
     */
    public void quit() {
        driver.quit();
        logCustom.info("退出浏览器");
    }

    /**
     * 浏览器前进
     */
    public void forward() {
        driver.navigate().forward();
        logCustom.info("浏览器前进");
    }

    /**
     * 浏览器后退
     */
    public void back() {
        driver.navigate().back();
        logCustom.info("浏览器后退");
    }

    /**
     * 刷新浏览器
     */
    public void refresh() {
        driver.navigate().refresh();
        logCustom.info("浏览器刷新");
    }

    public WebElement findElement(final Locator locator) throws IOException {
        /**
         * 查找某个元素等待几秒
         */
        Waitformax(Integer.valueOf(locator.getWaitSec()));
        WebElement webElement;
        webElement = getElement(locator);
        return webElement;


    }

    public void Waitformax(int t) {
        driver.manage().timeouts().implicitlyWait(t, TimeUnit.SECONDS);
    }

    /**
     * 通过定位信息获取元素
     *
     * @param locator
     * @return
     * @throws NoSuchElementException
     */
    public WebElement getElement(Locator locator) {
        /**
         * locator.getElement(),获取对象库对象定位信息
         */
        //locator=getLocator(locatorMap.get(key));
        WebElement webElement;
        switch (locator.getBy()) {
            case xpath:
                //logCustom.info("find element By xpath");
                webElement = driver.findElement(By.xpath(locator.getElement()));
                /**
                 * 出现找不到元素的时候，记录日志文件
                 */
                break;
            case id:
                //logCustom.info("find element By xpath");
                webElement = driver.findElement(By.id(locator.getElement()));
                break;
            case cssSelector:
                //logCustom.info("find element By cssSelector");
                webElement = driver.findElement(By.cssSelector(locator.getElement()));
                break;
            case name:
                //logCustom.info("find element By name");
                webElement = driver.findElement(By.name(locator.getElement()));
                break;
            case className:
                //logCustom.info("find element By className");
                webElement = driver.findElement(By.className(locator.getElement()));
                break;
            case linkText:
                //logCustom.info("find element By linkText");
                webElement = driver.findElement(By.linkText(locator.getElement()));
                break;
            case partialLinkText:
                //logCustom.info("find element By partialLinkText");
                webElement = driver.findElement(By.partialLinkText(locator.getElement()));
                break;
            case tagName:
                //logCustom.info("find element By tagName");
                webElement = driver.findElement(By.partialLinkText(locator.getElement()));
                break;
            default:
                //logCustom.info("find element By xpath");
                webElement = driver.findElement(By.xpath(locator.getElement()));
                break;

        }
        return webElement;
    }


}
